<template>
	<view>
		<image class="logo" src="/static/image/logo/logo.jpg"></image>

		<view class="login-warp">
			<view class="login-item">
				<navigator class="btn" url="/pages/subpages/account/login-subpage">手机号登陆</navigator>
			</view>
			<view class="auth-row flex-box " v-if="hasProvider">
				<view class="auth-image flex-item" v-for="provider in providerList" :key="provider.value">

					<!--  #ifdef APP-PLUS -->
					<image class="img" :src="provider.image" @click="authLogin(provider.value)"></image>
					<!--  #endif -->

					<!--  #ifdef MP-WEIXIN -->
					<button class="log-btn" open-type="getUserInfo" @getuserinfo="getUserInfo">
						<image class="img" v-if="provider.value == 'weixin'" :src="provider.image" ></image>
					</button>
					<!--  #endif -->

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hasProvider: false,
				providerList: []
			}
		},
		onLoad() {
			this._getProvider();
		},
		methods: {
			// 微信授权获取信息 获取 wx.getUserInfo 接口后续将不再出现授权弹窗，请注意升级
			getUserInfo(res) {
				if (!res.detail.iv) {
					uni.showToast({
						icon: "none",
						title: "您取消了授权,登录失败"
					});
					return false;
				}
				// 用户授权，获取用户基本信息和加密数据（encryptedData、iv），后台进行查询配对用户，生成userInfo返回前台
				// 这里有一个验证用户接口
				setTimeout(() => {
					let data = {};
					data.profile = JSON.parse(res.detail.rawData);
				
					data.profile.nickname =data.profile.nickName;
					console.log(data.profile.nickname)
					this.$store.dispatch('saveLogin', data)
					
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, 100);
			},

			authLogin(value) {
				uni.login({
					provider: value,
					success: loginRes => {
						uni.getUserInfo({
							provider: value,
							success: infoRes => {
								// 存储个人信息
								// 这里有一个验证用户接口

								let data = {};
								if (value == 'weixin') {
									infoRes.userInfo.nickname = infoRes.userInfo.nickName
								}
								data.profile = infoRes.userInfo
								this.$store.dispatch('saveLogin', data)

								// 登录完之后到首页
								uni.switchTab({
									url: '/pages/index/index'
								});
							}
						});

					}
				});
			},

			_getProvider() {
				const providerList = ['weixin', 'qq', 'sinaweibo'];
				uni.getProvider({
					service: 'oauth',
					success: res => {
						if (res.provider && res.provider.length) {
							for (let i = 0; i < res.provider.length; i++) {
								var tempProvider = res.provider[i];
								if (~providerList.indexOf(tempProvider)) {
									this.providerList.push({
										value: tempProvider,
										image: '/static/image/login/' + tempProvider + '.png'
									});
								}
							}
							this.hasProvider = true;
						}

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #DD2D20;
		height: 100%;
		overflow: hidden;
	}

	.logo {
		display: block;
		width: 130rpx;
		height: 130rpx;
		border-radius: 50%;

		margin: 200rpx auto 0;
	}

	.login-warp {
		text-align: center;
		height: 350rpx;
		position: fixed;
		width: 100%;
		bottom: 0;
		left: 0;

		.btn {
			margin: 30rpx auto 0;
			width: 550rpx;
			background-color: #fff;
			color: #DD2D20;
			height: 70rpx;
			line-height: 70rpx;
			border-radius: 50rpx;
		}

	}


	.auth-row {
		width: 450rpx;
		margin: 80rpx auto;

		.auth-image {
			height: 100rpx;
			line-height: 100rpx;
		}

		.img {
			width: 60rpx;
			height: 60rpx;
		}

		.log-btn {
			margin: 0;
			padding: 0;
			background: #DD2D20 !important;

			&::after {
				background-color: transparent;
				margin: 0;
				padding: 0;
				border: 0;
			}
		}

	}

	.log-btn {
		margin: 0;
		padding: 0;
		background: #fff !important;
		height: 48px;

		&::after {
			background-color: transparent;
			margin: 0;
			padding: 0;
			border: 0;
		}
	}
</style>
